/*!
    \copyright  Copyright (c) 2025 Qualcomm Technologies International, Ltd.
                All Rights Reserved.
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file
    @{
        \ingroup    adkfs
        \brief      AdkFs provider initialization handling
*/
#include "adkfs_provider_init.h"

#include <adkfs_provider_if.h>

#include <logging.h>

#include <message.h>
#include <panic.h>

static void adkFs_ProviderInitHandler(Task task, MessageId id, Message message);

static TaskData providers_init_task = {.handler = adkFs_ProviderInitHandler };

static struct {
    Task task;
    MessageId id;
    int pending_async_inits_count;
    void* pending_async_inits[ADKFS_MAX_PROVIDERS];
} app_init_info;

static void adkFs_ProviderInitHandler(Task task, MessageId id, Message message)
{
    UNUSED(task);
    UNUSED(message);

    DEBUG_LOG_ALWAYS("adkFs_ProviderInitHandler: enum:adkfs_provider_init_msgs_t:0x%04x", id);
    switch(id)
    {
        case ADKFS_PROVIDER_INIT_MSGS_PROVIDER_INIT_COMPLETE:
        {
            app_init_info.pending_async_inits_count--;
        }
        break;

        case ADKFS_PROVIDER_INIT_MSGS_CHECK_ALL_PROVIDERS_COMPLETE:
        break;

        default:
            Panic();
    }

    if ((0 <= app_init_info.pending_async_inits_count) && app_init_info.task)
    {
        MessageSend(app_init_info.task, app_init_info.id, NULL);
        app_init_info.task = NULL;
    }

    return;
}

void AdkFs_ProviderInitAddPendingAsync(void* async_init_fn)
{
    app_init_info.pending_async_inits[app_init_info.pending_async_inits_count++] = async_init_fn;
}

void AdkFs_ProviderInitSetAppInitTask(Task app_init_task, MessageId adkfs_init_complete_msg)
{
    PanicNotNull(app_init_info.task);
    app_init_info.task = app_init_task;
    app_init_info.id = adkfs_init_complete_msg;

    return;
}

Task AdkFs_ProviderInitGetTask(void)
{
    return &providers_init_task;
}